package com.code.origin.utils.datetime;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具类
 * @author: Huangds
 * @date: 2020-12-24 09:07
 * @version: v1.0
 */
public class DateUtils extends DateUtil {
	public final static String REG_YYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间加N秒
     * @author: Huangds
     * @date: 2020-12-24 09:07
     * @version: v1.0
     */
	public static Date dateAddSecond(Date date, Long second) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.SECOND, second.intValue());
		return c.getTime();
	}
    
    /**
     * 比较当前时间差距多少秒
	 * <pre>当前时间减去compareDate</pre>
     * @author: Huangds
     * @date: 2020-12-24 09:07
     * @version: v1.0
     */
	public static long compareNowTimeDistanceSeconds(Date compareDate) {
		return compareTimeDistanceSeconds(new Date(), compareDate);
	}
	
	/**
	 * 获取两个时间差距多少秒
	 * @author: Huangds
	 * @date: 2020-12-24 09:08
	 * @version: v1.0
	 */
	public static long compareTimeDistanceSeconds(Date beforeDate, Date compareDate) {
		long result = getSeconds(REG_YYMMDD_HHMMSS, compareDate) - getSeconds(REG_YYMMDD_HHMMSS, beforeDate);
		return result;
	}
	
	/**
	 * 获取两个时间差距多少分钟
	 * @author: Huangds
	 * @date: 2020-12-24 09:09
	 * @version: v1.0
	 */
	public static long compareTimeDistanceMinutes(Date beforeDate, Date compareDate) {
		long result = compareTimeDistanceSeconds(beforeDate, compareDate) ;
		return result / 60;
	}
	
	/**
	 * 格式化日期时间<br>
	 * 格式 yyyy-MM-dd HH:mm:ss
	 * @param timeStamp 时间戳字符串(毫秒(13位))
	 * @return String 格式化后的日期
	 * @author: Huangds
	 * @date: 2020-12-24 09:09
	 * @version: v1.0
	 */
	public static String formatDateTime2TimeStampString(String timeStamp) {
		if (null == timeStamp) {
			return null;
		}
		Long time = Long.valueOf(timeStamp);
		
		return formatDateTime2TimeStamp(time);
	}
	
	/**
	 * 格式化日期时间<br>
	 * 格式 yyyy-MM-dd HH:mm:ss
	 * @param timeStamp 时间戳字符串(毫秒(13位))
	 * @return String 格式化后的日期
	 * @author: Huangds
	 * @date: 2020-12-24 09:10
	 * @version: v1.0
	 */
	public static String formatDateTime2TimeStamp(Long timeStamp) {
		if (null == timeStamp) {
			return null;
		}
		Date date = new Date(timeStamp);
		return DatePattern.NORM_DATETIME_FORMAT.format(date);
	}
	
	/**
	 * 获取秒
	 * @author: Huangds
	 * @date: 2020-12-24 09:11
	 * @version: v1.0
	 */
	public static long getSeconds(String reg, Date date){
		long seconds = 0;
		if(date == null){
			return seconds;
		}
		seconds = new Long(date.getTime() / 1000L);
		return seconds;
	}
	
}
